﻿using System;
using System.Collections.Generic;
using System.Text;

namespace RedditCrawler
{
    class Utility
    {
        public static bool IsImageURL(String url)
        {
            return (url.EndsWith(".png") || url.EndsWith(".jpg") || url.EndsWith(".gif"));
        }

        public static String SanitizeText(String input)
        {
            input = input.Replace("&quot;", "'");
            input = input.Replace("&amp;", "&");
            String result = "";
            foreach (char c in input)
            {
                if ((c >= 'a' && c <= 'z') ||
                    (c >= 'A' && c <= 'z') ||
                    (c >= '0' && c <= '9') ||
                    (c == ' ') || (c == '-') ||
                    (c == '[') || (c == ']') ||
                    (c == '.') || (c == ',') ||
                    (c == '\'') || (c == '+') ||
                    (c == '&') || (c == '='))
                {
                    result += new String(c, 1);
                }
            }
            return result;
        }

        public static List<String> MultiSplit(String input, String startsWith, String endsWith)
        {
            String[] startCandidates = input.Split(new string[] { startsWith }, StringSplitOptions.RemoveEmptyEntries);

            bool firstSkipped = input.StartsWith(startsWith);
            List<String> result = new List<String>();
            foreach (String s in startCandidates)
            {
                if (!firstSkipped) firstSkipped = true;
                else if (s.Contains(endsWith))
                {
                    String[] parts = s.Split(new string[] { endsWith }, StringSplitOptions.RemoveEmptyEntries);
                    result.Add(parts[0]);
                }
            }
            return result;
        }

        public static int Mod(int x, int M)
        {
            if (M == 1) return 0;
            if (x < 0) return M + (x % M);
            else return x % M;
        }
    }
}
